// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.sync.controller;

import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.material.controller.request.QueryMaterialReq;
import com.liuxinlong.modules.material.service.MaterialService;
import com.liuxinlong.modules.sync.service.SyncService;
import com.liuxinlong.utils.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述
 * <p>
 * 此类关联设计的功能
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年2月22日
 */
@RestController
@RequestMapping("/sync")
public class SyncController {

	@Autowired
	private SyncService syncService;

	@Autowired
	private MaterialService materialService;

	/**
	 * 分页查询物料列表
	 *
	 * @param req 请求信息
	 * @return 物料分页列表
	 */
	@PostMapping("page")
	public BaseResp syncMaterial(@RequestBody QueryMaterialReq req) {
		Map<String, Object> queryParam = new HashMap<>();
		queryParam.put("startNum", req.getStartNum());
		queryParam.put("pageSize", req.getPageSize());
		queryParam.put("materialNo", ObjectUtils.isEmpty(req.getMaterialNo()) ? "" : StringUtils.upperCase(req.getMaterialNo()));
		queryParam.put("desc", req.getName());
		List<Map<String, Object>> dataList = syncService.syncMaterial(queryParam);
		int total = syncService.countMaterial(queryParam);
		BaseResp resp = new BaseResp();
		resp.setDataList(dataList);
		resp.setTotalNum(total);
		return resp;
	}

	/**
	 * 分页查询最终父物料列表
	 *
	 * @param req 请求信息
	 * @return 最终父物料分页列表
	 */
	@PostMapping("parent")
	public BaseResp parentMaterial(@RequestBody QueryMaterialReq req) {

		List<Map<String,Object>> dataList = materialService.getFinalParentMaterial(req.getMaterialNo(),req.getRegion());
		BaseResp resp = new BaseResp();
		resp.setDataList(dataList);
		resp.setRetMessage("分页查询最终父物料列表成功");
		return resp;
	}

	/**
	 * 查询物料相关信息
	 *
	 * @param req 请求信息
	 * @return 物料相关信息
	 */
	@PostMapping("expand")
	public BaseResp getMaterialExpand(@RequestBody QueryMaterialReq req) {
		Map<String,Object> dataMap = materialService.getMaterialExpand(req.getMaterialNo(),req.getRegion());
		BaseResp resp = new BaseResp();
		resp.setDataMap(dataMap);
		resp.setRetMessage("查询物料相关信息成功");
		return resp;
	}
}
